package com.mhy.netspeed;

import android.net.TrafficStats;
import android.os.Handler;
import android.os.Looper;

/**
 * =====================================
 * 作    者：mahongyin
 * 项    目：HYNetSpeedUtils
 * 目    录：com.mhy.netspeed
 * 创建日期：2024/8/22 上午9:15
 * 描    述：1字节 (B) = 8比特 (b); 8bit=1Byte; 1KB=1024B
 * 使    用：
 * =====================================
 */
public class HyNetSpeedUtils {
    private Handler handler;
    private long previousRxBytes = 0;
    private long previousTxBytes = 0;
    private long previousTimeStamp = 0;
    //默认每1秒更新一次
    private long delayTime = 1000;
    private OnNetSpeedListener onNetSpeedListener;
    private boolean isPaused = false;
    private static volatile HyNetSpeedUtils instance;

    public static HyNetSpeedUtils getInstance() {
        if (instance == null) {
            synchronized (HyNetSpeedUtils.class) {
                if (instance == null) {
                    instance = new HyNetSpeedUtils();
                }
            }
        }
        return instance;
    }

    public interface OnNetSpeedListener {
        /**
         * @param mSpeed " B/s"
         */
        void onUpSpeed(long mSpeed);

        void onDownSpeed(long mSpeed);
    }

    //定期更新网络速度
    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            long currentRxBytes = TrafficStats.getTotalRxBytes();
            long currentTxBytes = TrafficStats.getTotalTxBytes();
            long currentTimeStamp = System.currentTimeMillis();

            long rxBytesDelta = currentRxBytes - previousRxBytes;
            long txBytesDelta = currentTxBytes - previousTxBytes;
            long timeDelta = currentTimeStamp - previousTimeStamp;

            long rxSpeed = (rxBytesDelta * 1000) / timeDelta; // 每秒字节数
            long txSpeed = (txBytesDelta * 1000) / timeDelta; // Bytes per second
            if (onNetSpeedListener != null) {
                onNetSpeedListener.onUpSpeed(txSpeed);
                onNetSpeedListener.onDownSpeed(rxSpeed);
            }
            if (delayTime != 0) {
                previousRxBytes = currentRxBytes;
                previousTxBytes = currentTxBytes;
                previousTimeStamp = currentTimeStamp;
                // 每秒更新一次
                handler.postDelayed(this, delayTime);
            } else {//只1次
                stop();
            }
        }
    };

    /**
     * @param delay 多少毫秒 更新一次  如果是0则只获取1次
     */
    public void start(long delay, OnNetSpeedListener listener) {
        handler = new Handler(Looper.getMainLooper());
        onNetSpeedListener = listener;
        delayTime = delay;

        previousRxBytes = TrafficStats.getTotalRxBytes();
        previousTxBytes = TrafficStats.getTotalTxBytes();
        previousTimeStamp = System.currentTimeMillis();

        handler.postDelayed(runnable, delayTime);
    }

    public void resume() {
        if (isPaused) {
            previousRxBytes = TrafficStats.getTotalRxBytes();
            previousTxBytes = TrafficStats.getTotalTxBytes();
            previousTimeStamp = System.currentTimeMillis();

            handler.postDelayed(runnable, delayTime);
        }
    }

    public void pause() {
        isPaused = true;
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
    }

    public void stop() {
        pause();
        handler = null;
        previousRxBytes = 0;
        previousTxBytes = 0;
        previousTimeStamp = 0;
        onNetSpeedListener = null;
    }

}
